import json

cs_handler = 'raw_response'
content_type = 'application/json'

path = json.loads(cs_form.get('path', '[]'))
course = path[0]
path = path[1:]

user = cs_form.get('user', None)
question = cs_form.get('question', None)
data = json.loads(cs_form.get('data', "{}"))
#response = json.dumps(list(data.keys()))
#response = json.dumps({'path':path, 'course':course, 'user':user, 'data':data, 'question':question})

if 'cs_user_info' not in globals() or cs_user_info.get('username', None) != '__coderunner_user__':
    response = json.dumps({'ok':False, 'error': 'You are not allowed to do this'})
else:
    # add a new entry corresponding to this action in a special log
    csm_cslog.update_log(course, user, '.'.join(['coderunner_results'] + path), data)

    # update the problem state with the info from the completed run
    def transformer(x):
        scores = x.get('scores', {})
        scores[question] = data['score']
        x['%s_message' % question] = data['response']
        return x
    csm_cslog.modify_most_recent(course, user, '.'.join(path + ['problemstate']), {}, transformer)
    response = json.dumps({'ok': True})
